package com.ppch.zerocodegenerator.langgraph4j.node;

import com.ppch.zerocodegenerator.langgraph4j.ai.ImageCollectionService;
import com.ppch.zerocodegenerator.langgraph4j.model.ImageCategoryEnum;
import com.ppch.zerocodegenerator.langgraph4j.model.ImageResource;
import com.ppch.zerocodegenerator.langgraph4j.model.WorkflowContext;
import com.ppch.zerocodegenerator.langgraph4j.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.bsc.langgraph4j.action.AsyncNodeAction;
import org.bsc.langgraph4j.prebuilt.MessagesState;

import java.util.Arrays;
import java.util.List;

import static org.bsc.langgraph4j.action.AsyncNodeAction.node_async;

@Slf4j
public class ImageCollectorNode {
    /**
     * 创建图片收集工作节点
     * @return AsyncNodeAction
     */
    public static AsyncNodeAction<MessagesState<String>> create() {
        return node_async(state -> {
            //获取工作流上下文对象
            WorkflowContext context = WorkflowContext.getContext(state);
            log.info("执行节点: 图片收集");
            String collectImages = null;
            try {
                // 获取AI图片收集服务 ImageCollectionService
                ImageCollectionService imageCollectionService = SpringContextUtil.getBean(ImageCollectionService.class);
                // 调用AI图片收集服务，收集图片
                collectImages = imageCollectionService.collectImages(context.getOriginalPrompt());
            } catch (Exception e) {
                log.error("图片收集失败:{}", e.getMessage(),e);
            }
            // 将该工作节点的数据更新到工作流上下文对象WorkflowContext中
            context.setCurrentStep("图片收集");
            context.setImageListStr(collectImages);
            log.info("图片收集完成: {}", collectImages);
            // 将状态追加到AgentState中（更新全局共享状态AgentState）
            return WorkflowContext.saveContext(context);
        });
    }
}
